<?php
namespace jy_farmer;
defined('BY_JYA') or exit('error');
/*
	配送费
*/
class dispatch extends \plugin{
	public function get_freight($pcd='',$throw_error=false,$showbaoyou=false,$weight=0,$total=0){
		//$this->throw_error=$throw_error;
		$total_price=0;
		$baoyoufee=0;
		$basic=$this->plugin_setting('basic');
		$newlist=array();
		if($dispatchtype==1){
				//统一运费
				$total_price=$basic['dispatchfee'];
		}elseif($basic['template_id']){
				$get_dispatch_price=$this->get_dispatch_price($pcd,$basic['template_id'],$weight,$total);				
				$total_price=$get_dispatch_price['fee'];
				$outarea=$get_dispatch_price['outarea'];
		}else{
				$default_template_id=pdo_getcolumn('jy_weishop_dispatch',array('uniacid'=>$_SESSION['uniacid'],'isdefault'=>1),'id');
				$get_dispatch_price=$this->get_dispatch_price($pcd,$default_template_id,$weight,$total);
				$total_price=$get_dispatch_price['fee'];
				$outarea=$get_dispatch_price['outarea'];				
		}
		$baoyou_man=$saleset['baoyou_status'] && $saleset['baoyou_man']>0?$saleset['baoyou_man']:0;
		if($showbaoyou){
				return array('fee'=>$total_price,'outarea'=>$outarea,'addbaoyoufee'=>0,'isonebaoyou'=>$isbaoyou,'baoyou_man'=>$baoyou_man);
		}else{
				return $total_price;
		}
	}
	public function get_dispatch_price($pcd='',$template_id,$weight=0,$total=0){
		$dispatch=pdo_get('jy_weishop_dispatch',array('id'=>$template_id));
		if($dispatch['isdispatcharea']==0 && $pcd){
			//不配送区域
			$nodispatchareas=json_decode($dispatch['nodispatchareas'],true);
			if($this->check_pcd($pcd,$nodispatchareas)){
				$outarea=1;
				exi('您的收货地址不在商品配送范围！','error');
			}
		}
		if($dispatch['isdispatcharea']==1 && $pcd){
			//只配送区域
			$onlydispatchareas=json_decode($dispatch['onlydispatchareas'],true);
			if(!$this->check_pcd($pcd,$onlydispatchareas)){
				$outarea=1;
				exi('您的收货地址不在商品配送范围！','error');
			}
		}
		if($dispatch['type']==0){
			//按重量计费
			$count=$weight;
		}else{
			$count=$total;
		}
		$price=0;
		if($dispatch['isdispatcharea']<=1 || !$pcd){
			//统一计费
			$price=$dispatch['shouprice'];
			if($dispatch['xuweight'] && $dispatch['xuprice'] && $count>$dispatch['shouweight']){
				$price+=ceil(($count-$dispatch['shouweight'])/$dispatch['xuweight'])*$dispatch['xuprice'];
			}
			$DATA=ijson_encode($dispatch);
		}else{			
			//自定义配送区域，寻找匹配的配送区域设置
			$areas=json_decode($dispatch['areas'],true);
			if($areas){
				foreach($areas as $av){
					$av['area']=$av['area']?json_decode($av['area'],true):array();
					if($this->check_pcd($pcd,$av['area'])){
						//区域命中
						$isarea=true;
						$price=$av['shouprice'];
						if($av['xu'] && $av['xuprice'] && $count>$av['shou']){
							$price+=ceil(($count-$av['shou'])/$av['xu'])*$av['xuprice'];
						}
						break;
					}
				}
				$outarea=$isarea?0:1;
			}
		}
		return array('fee'=>$price,'outarea'=>$outarea);
	}
	public function up_dispatch($post,$addressid=0,$weight=0,$total=0){
			if(intval($addressid)){
				$post['address']=pdo_get('core_address',array('mid'=>$_SESSION['uid'],'id'=>intval($addressid)));
				$post['addressid']=intval($addressid);
			}else{
				$member_address=pdo_getall('core_address',array('mid'=>$_SESSION['uid']));
				if($member_address){
					foreach($member_address as &$address){
						if($address['isdefault']){
							$post['addressid']=$address['id'];
							$post['address']=$address;
							break;
						}
					}
					if(!$post['addressid']){
						$post['addressid']=$member_address['0']['id'];
						$post['address']=$member_address['0'];
						
					}
					
				}
			}
			if($post['address']){
				$post['address']['pcd']=json_decode($post['address']['pcd'],true);
				try{
					$get_freight=$this->get_freight($post['address']['pcd'],true,true,$weight,$total);
					if($get_freight['outarea']){
							$post['outarea']=1;
					}
					$post['dispatchprice']=$get_freight['fee'];
					$post['isonebaoyou']=$get_freight['isonebaoyou'];
					$post['addbaoyoufee']=$get_freight['addbaoyoufee'];
					$post['baoyou_man']=$get_freight['baoyou_man']?:0;
					$post['lateye_fee']=$get_freight['lateye_fee']?:0;
					$post['teshufee']=$get_freight['teshufee']?:0;
				}catch(Exception $e){
					unset($post['address'],$post['addressid']);
					$post['dispatchprice']=0;
					$post['isnoarea']=1;
				}
			}else{
				$get_freight=$this->get_freight($post['list'],true,true,$weight,$total);
				$post['dispatchprice']=$get_freight['fee'];
				$post['isonebaoyou']=$get_freight['isonebaoyou'];
				$post['addbaoyoufee']=$get_freight['addbaoyoufee'];
				$post['baoyou_man']=$get_freight['baoyou_man']?:0;
				$post['lateye_fee']=$get_freight['lateye_fee']?:0;
				$post['teshufee']=$get_freight['teshufee']?:0;
			}
			//多少钱起送
			$post['no_out_price']=$get_freight['no_out_price']?:0;
			$get_freight['juli']=price($get_freight['juli']);
			$post['isnocity']=$get_freight['iscity']?0:1;
			return array('post'=>$post,'get_freight'=>$get_freight);
	}
	public function check_pcd($pcd='',$area){
		//检查行政区划是否存在于区域数组
		//如果没有传地址，就默认成功
		if(!$pcd){
			return true;
		}
		if(!$area[$pcd['province']]){
			return false;
		}
		if($area[$pcd['province']]===true){
			return true;
		}
		if(!$area[$pcd['province']][$pcd['city']]){
			return false;
		}
		if($area[$pcd['province']][$pcd['city']]===true){
			return true;
		}
		if(!$area[$pcd['province']][$pcd['city']][$pcd['district']]){
			return false;
		}
		return true;
	}
}